package org.qiaice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.qiaice.entity.Setmeal;
import org.qiaice.entity.SetmealDish;
import org.qiaice.entity.dto.SetmealDTO;
import org.qiaice.entity.dto.SetmealPageQueryDTO;
import org.qiaice.entity.vo.SetmealVO;
import org.qiaice.exception.BaseException;
import org.qiaice.mapper.SetmealMapper;
import org.qiaice.result.PageResult;
import org.qiaice.service.SetmealDishService;
import org.qiaice.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    private final SetmealDishService setmealDishService;

    @Override
    public SetmealVO queryById(Long id) {
        SetmealVO setmealVO = baseMapper.queryById(id);
        setmealVO.setSetmealDishes(setmealDishService.lambdaQuery().eq(SetmealDish::getSetmealId, id).list());
        return setmealVO;
    }

    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(1);
        save(setmeal);
        Long setmealId = setmeal.getId();
        setmealDTO.getSetmealDishes()
                .forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishService.saveBatch(setmealDTO.getSetmealDishes());
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        ids = lambdaQuery().in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 0).list()
                .stream().map(Setmeal::getId).toList();
        if (ids.isEmpty()) {
            throw new BaseException("在删除套餐之前请先停售该套餐");
        }
        removeByIds(ids);
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId, ids));
    }

    @Override
    public PageResult<SetmealVO> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        IPage<SetmealVO> setmealVOS = baseMapper.pageQuery(setmealPageQueryDTO.getCategoryId(),
                setmealPageQueryDTO.getName(), setmealPageQueryDTO.getStatus(),
                Page.of(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize()));
        return new PageResult<>(setmealVOS.getTotal(), setmealVOS.getRecords());
    }

    @Override
    public void change(SetmealDTO setmealDTO) {
        Setmeal setmeal = Setmeal.builder().build();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        updateById(setmeal);
        Long setmealId = setmeal.getId();
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, setmealId));
        setmealDTO.getSetmealDishes().forEach(item -> item.setSetmealId(setmealId));
        setmealDishService.saveBatch(setmealDTO.getSetmealDishes());
    }
}
